package com.dlc.apk.ui.fragment

import android.content.Intent
import com.dlc.apk.R
import com.dlc.apk.base.BaseFragment
import com.dlc.apk.core.*
import com.dlc.apk.databinding.FragmentTestingBinding
import com.dlc.apk.service.staff.IStaffService
import com.dlc.apk.service.staff.impl.StaffService
import com.dlc.apk.ui.activity.DebugActivity
import com.dlc.apk.ui.activity.MainActivity
import com.dlc.apk.ui.model.DeviceInfoModel
import com.dlc.apk.utils.CacheHelper
import com.dlc.apk.utils.DeviceUtil
import com.dlc.apk.utils.FlowUtil
import com.dlc.apk.utils.LogPlus

class TestingFragment : BaseFragment() {
    private lateinit var deviceInfoModel: DeviceInfoModel
    private lateinit var staffService: IStaffService

    override fun getLayoutId(): Int {
        return R.layout.fragment_testing
    }

    override fun initViewModels() {
        deviceInfoModel = DeviceInfoModel()
        val deviceInfo = CacheHelper.getDeviceInfo()
        deviceInfoModel.deviceNo.set(DeviceUtil.androidId)
        deviceInfoModel.address.set(deviceInfo.address)
    }

    override fun initServices() {
        staffService = StaffService()
    }

    override fun init() {
        val fragmentBinding = getViewDataBinding<FragmentTestingBinding>()!!
        fragmentBinding.deviceInfoModel = deviceInfoModel
        fragmentBinding.appVersion = DeviceUtil.packageVersion
        fragmentBinding.tvToDebug.setOnClickListener { v ->
            toDebug()
        }

        fragmentBinding.tvTesting.setOnClickListener {
            doShipment(1, null)
        }

        fragmentBinding.tvAir.setOnClickListener {

        }

        (getParentActivity() as MainActivity).showLogoBar()
    }

    private fun doShipment() {

    }


    private fun toDebug() {
        getParentActivity()?.startActivity(Intent(getParentActivity(), DebugActivity::class.java))
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    override fun onDestroyView() {
        super.onDestroyView()
    }

    override fun onDetach() {
        super.onDetach()
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (hidden) {

        }
    }

    /**
     * 出货
     */
    private fun doShipment(cargoNumber: Int, attachData: Any? = null) {
        val shipmentTasks = ArrayList<ShipmentTaskHandler.ShipmentTask>()
        var taskCompleteCount = 0

        if (true) {
            val flowNo = FlowUtil.nextFlow()
            val cargoNumberType = 0x01//0x00:行列方式 0x01:编号方式
            val actionCargoRowNumberOrNo1 = cargoNumber
            val actionCargoColumnNumber1 = 0
            val actionCargoRowNumberOrNo2 = 0
            val actionCargoColumnNumber2 = 0
            val actionCargoRowNumberOrNo3 = 0
            val actionCargoColumnNumber3 = 0
            val actionCargoRowNumberOrNo4 = 0
            val actionCargoColumnNumber4 = 0
            val actionCargoRowNumberOrNo5 = 0
            val actionCargoColumnNumber5 = 0
            val maxActionTime = 5//单位1秒，当为0时，表示采用默认时间。默认为5秒
            val pickTimeOut = 0//单位秒，货物到达出货口到货物被取走的超时时间。超过此时间取货锁将自动上锁，为0表示不带取货锁
            val theftBaffleTimeout = 30//单位秒，防盗挡板开关的超时时间，超过此时间还未到位，表示防盗挡板出错。
            val attachData = attachData //当前出货透传数据

            shipmentTasks.add(ShipmentTaskHandler.ShipmentTask(flowNo, cargoNumberType, actionCargoRowNumberOrNo1, actionCargoColumnNumber1, actionCargoRowNumberOrNo2, actionCargoColumnNumber2, actionCargoRowNumberOrNo3, actionCargoColumnNumber3, actionCargoRowNumberOrNo4, actionCargoColumnNumber4, actionCargoRowNumberOrNo5, actionCargoColumnNumber5, maxActionTime, pickTimeOut, theftBaffleTimeout, attachData, 0, -1, -1, object : ShipmentTaskHandler.IShipmentTaskCallback {
                override fun onSuccess(shipmentTask: ShipmentTaskHandler.ShipmentTask) {
                    val isShipmentSuccess = DeviceSerialManager.instance.isShipmentSuccess(shipmentTask.errCode, shipmentTask.firstShipmentResult)
                    val shipmentResultStr = if (isShipmentSuccess) "成功" else "失败"
                    LogPlus.e("###货道出货完成###货道号->${shipmentTask.actionCargoRowNumberOrNo1},错误代码->${shipmentTask.errCode},第1个货道掉货结果->${shipmentTask.firstShipmentResult},认定出货结果->${shipmentResultStr}")
                    taskCompleteCount += 1

                    val isAllDone = taskCompleteCount >= shipmentTasks.size

                    if (!isAllDone) {
                        LogPlus.e("###此订单出货结果进度,总数:${shipmentTasks.size},已完成数量->${taskCompleteCount}")
                        return
                    }
                    LogPlus.e("###当前订单所有出货任务完成###")
                }

                override fun onFailed(shipmentTask: ShipmentTaskHandler.ShipmentTask, msg: String) {
                    LogPlus.e("###设备出货失败###货道号->${shipmentTask.actionCargoRowNumberOrNo1}, msg->$msg")
                    taskCompleteCount += 1

                    val isAllDone = taskCompleteCount >= shipmentTasks.size
                    if (!isAllDone) {
                        LogPlus.e("###此订单出货结果进度,总数:${shipmentTasks.size},已完成数量->${taskCompleteCount}")
                        return
                    }
                    LogPlus.e("###当前订单所有出货任务完成###")
                }
            }))
        }

        for (shipmentTask in shipmentTasks) {
            val flowNo = shipmentTask.flowNo
            val cargoNumberType = shipmentTask.cargoNumberType
            val actionCargoRowNumberOrNo1 = shipmentTask.actionCargoRowNumberOrNo1
            val actionCargoColumnNumber1 = shipmentTask.actionCargoColumnNumber1
            val actionCargoRowNumberOrNo2 = shipmentTask.actionCargoRowNumberOrNo2
            val actionCargoColumnNumber2 = shipmentTask.actionCargoColumnNumber2
            val actionCargoRowNumberOrNo3 = shipmentTask.actionCargoRowNumberOrNo3
            val actionCargoColumnNumber3 = shipmentTask.actionCargoColumnNumber3
            val actionCargoRowNumberOrNo4 = shipmentTask.actionCargoRowNumberOrNo4
            val actionCargoColumnNumber4 = shipmentTask.actionCargoColumnNumber4
            val actionCargoRowNumberOrNo5 = shipmentTask.actionCargoRowNumberOrNo5
            val actionCargoColumnNumber5 = shipmentTask.actionCargoColumnNumber5
            val maxActionTime = shipmentTask.maxActionTime
            val pickTimeOut = shipmentTask.pickTimeOut
            val theftBaffleTimeout = shipmentTask.theftBaffleTimeout
            val attachData = shipmentTask.attachData
            val callback = shipmentTask.shipmentTaskCallback!!
            DeviceManager.instance.addShipmentTask(flowNo, cargoNumberType, actionCargoRowNumberOrNo1, actionCargoColumnNumber1, actionCargoRowNumberOrNo2, actionCargoColumnNumber2, actionCargoRowNumberOrNo3, actionCargoColumnNumber3, actionCargoRowNumberOrNo4, actionCargoColumnNumber4, actionCargoRowNumberOrNo5, actionCargoColumnNumber5, maxActionTime, pickTimeOut, theftBaffleTimeout, attachData, callback)
        }
    }
}